package com.t209.hibernatelearn.service;

import com.t209.hibernatelearn.dao.DeptDao;
import com.t209.hibernatelearn.entity.Dept;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

public class DeptBiz {
    private DeptDao deptDao=new DeptDao();

    public DeptDao getDeptDao(){
        return deptDao;
    }

    public void setDeptDao(DeptDao deptDao){
        this.deptDao=deptDao;
    }

    public Dept findDeptById(Byte id){
        Transaction transaction=null;
        Dept res=null;
        try {
            transaction=deptDao.currentSession().beginTransaction();
            res=this.deptDao.get(id);
            transaction.commit();
        }catch (HibernateException e){
            e.printStackTrace();
            if(transaction!=null) transaction.rollback();
        }finally {
            return res;
        }
    }

    public Dept findDeptByLoad(Byte id){
        Transaction transaction=null;
        Dept res=null;
        try {
            transaction=deptDao.currentSession().beginTransaction();
            res=this.deptDao.load(id);
            transaction.commit();
        }catch (HibernateException e){
            e.printStackTrace();
            if(transaction!=null) transaction.rollback();
        }finally {
            return res;
        }
    }

    public void addNewDept(Dept dept){
        Transaction transaction=null;
        try {
            transaction=deptDao.currentSession().beginTransaction();
            deptDao.save(dept);
            transaction.commit();
        }catch (HibernateException e){
            e.printStackTrace();
            if(transaction!=null) transaction.rollback();
        }
    }

    public void updateDept(Dept dept){
        Transaction transaction=null;
        try {
            transaction=deptDao.currentSession().beginTransaction();
            //更新数据,通过load加载数据,修改加载后的值即可,区别于get
            Dept load=deptDao.load(dept.getDeptNo());
            load.setDname(dept.getDname());
            load.setLoc(dept.getLoc());
            transaction.commit();
        }catch (HibernateException e){
            e.printStackTrace();
            if(transaction!=null) transaction.rollback();
        }
    }

    public void updateDept2(Dept dept){
        Transaction transaction=null;
        try {
            transaction=deptDao.currentSession().beginTransaction();
            //更新数据,通过update更新数据,修改加载后的值即可,区别于get
            deptDao.update(dept);
            transaction.commit();
        }catch (HibernateException e){
            e.printStackTrace();
            if(transaction!=null) transaction.rollback();
        }
    }

    public void deleteDept(Byte id){
        Transaction transaction=null;
        try {
            transaction=deptDao.currentSession().beginTransaction();
            //更新数据,通过update更新数据,修改加载后的值即可,区别于get
            Dept load=deptDao.load(id);
            deptDao.delete(load);
            transaction.commit();
        }catch (HibernateException e){
            e.printStackTrace();
            if(transaction!=null) transaction.rollback();
        }
    }


}
